﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace MTQ.DAL.DBView
{
    public class ServerConnect
    {
        private string sqlConnect;
        public ServerConnect() {            
            sqlConnect = ConfigurationManager.ConnectionStrings["MTQFramework"].ToString(); 
        }
        /// <summary>
        /// 获取数据库所有用户表
        /// </summary>
        /// <returns></returns>
        public ArrayList getAllTable()
        {
            string strSql = "Select Name FROM SysObjects Where XType='U' orDER BY Name ";

            return getListName(strSql);
        }

        public ArrayList getAllView()
        {            
            string strSql = "Select Name FROM SysObjects Where XType='V' orDER BY Name ";

            return getListName(strSql);
        }


        private ArrayList getListName(string strSql)
        {
            ArrayList list = new ArrayList();
            DataTable dt = null;
            try
            {
                dt = SqlHelper.ExecuteDataset(this.sqlConnect, CommandType.Text, strSql).Tables[0];
            }
            catch
            {
                return null;
            }
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(dr["Name"].ToString());
            }
            return list;
        }

        private ArrayList getListName(string strSql, params SqlParameter[] sqlPar)
        {
            ArrayList list = new ArrayList();
            DataTable dt = null;
            try
            {
                dt = SqlHelper.ExecuteDataset(this.sqlConnect, CommandType.Text, strSql, sqlPar).Tables[0];
            }
            catch
            {
                return null;
            }
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(dr["Name"].ToString());
            }
            return list;
        }

    }
}
